/****************************************************************************/
/*
* test sample for xml API.
*/
/****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cms_common.h"
#include "cms_log.h"
#include "libmdm.h"
#include "cms_msg.h"
#include "cms_mdm.h"

#define ENCRYKEY 0x20191226

const char processArray[][64] = {
	CMS_CORE,
	CMS_DB,
	CMS_TASK,
};

cmsXmlCb_t *xmlShmCb= NULL;
char moduleName[64] = {0};

static int cms_sendMsgToProcess(cmsMsgCb_t *pReqMsg, const char *processName)
{
	int msqId = -1;
	int key = -1;
	int ret = -1;

	if(!strcmp(processName, CMS_CORE))
		genKeyByName(CMS_CORE, &key, NULL);
	else if(!strcmp(processName, CMS_DB))
		genKeyByName(CMS_DB, &key, NULL);
	else if(!strcmp(processName, CMS_TASK)){
		key = CMSTASK_STATS_KEY;
	}else if(!strcmp(processName, CMS_LOOPDETECT))
		genKeyByName(CMS_LOOPDETECT, &key, NULL);
	
	if((msqId = msgget(key, 0666)) == -1){
		CMS_LOG(LL_ERROR,"get dst module msqId failed, key 0x%x.\n", key);
		return -1;
	}
	
	ret = cms_sendMsg(pReqMsg, msqId);
	
	return ret;
}


static int setProcessLogLevel(char *processName, int logLevel)
{
	cmsMsgCb_t *pReqMsg = NULL;
	FILE *fp = NULL;
	char cmdStr[64] = {0};
	int i = 0;
	int setFlag = 0;
	
	if(processName == NULL)
		return -1;

	for(i = 0; i < (sizeof(processArray)/sizeof(processArray[0])); i++){
		if(!strcmp(processArray[i], processName)){
			/*store logLevel to file*/
			if(access(CMS_LOG_LEVEL_FILE, F_OK) == -1){
				fp = fopen(CMS_LOG_LEVEL_FILE, "w+");
				if(fp){
					sprintf(cmdStr, "%s=%d\n", processName, logLevel);
					fputs(cmdStr, fp);
					fclose(fp);
				}
			}else{
				fp = fopen(CMS_LOG_LEVEL_FILE, "r+");
				if(fp){
					while(fgets(cmdStr, sizeof(cmdStr), fp) != NULL){
						if(strstr(cmdStr, processName)){
							memset(cmdStr, 0, sizeof(cmdStr));
							sprintf(cmdStr, "%s=%d\n", processName, logLevel);
							fseek(fp, -strlen(cmdStr), SEEK_CUR);
							fputs(cmdStr, fp);
							setFlag = 1;
							break;
						}
					}

					if(setFlag == 0){
						snprintf(cmdStr, sizeof(cmdStr), "%s=%d\n", processName, logLevel);
						fputs(cmdStr, fp);
					}
					fclose(fp);
				}
			}
			
			/*create req msg and set to process to set log level*/
			pReqMsg = cms_createRequestMsg(0);
			if(pReqMsg == NULL){
				CMS_LOG(LL_ERROR,"save xml fail, create request msg fail.");
				return -1;
			}

			cms_requestSetLog(pReqMsg, logLevel);
			
			cms_sendMsgToProcess(pReqMsg, processName);
			cms_msgFree(pReqMsg);

			return 0;
		}
	}
    CMS_LOG(LL_ERROR, "not support process %s", processName);

	return -1;
}

int parseRcvMsg(cmsMsgCb_t *pRecvMsg, cmsXmlCb_t *xmlShmCb){
	cmsMsg_header_t* pHeader;
	cmsMsg_action_t* pAction;
	void *offsetValue = NULL;
	cmsoffset_t offset = 0;
	cmsuint32 actCount;
	cmsuint32 type;
	void *getValue;
	int ret = 0;

	pHeader = cms_parseMsgHeader(pRecvMsg);
	if(IS_NACK(pHeader->flag)){
		printf("transaction fail !!\n");
		cms_msgFree(pRecvMsg);
		return -1;
	}
	if(IS_ACK(pHeader->flag)){
		printf("transaction successful !!\n");
	}
	actCount = pHeader->actionNum;
	while(actCount != 0){
		pAction = cms_parseMsgAction(pRecvMsg);
		if(pAction == NULL){
			printf("cms parse msg action fail\n");
			cms_msgFree(pRecvMsg);
			return -1;
		}
		
		GET_TYPE(type, pAction->action->type);
		switch(type){
			case CMS_MSG_ACT_SEL:
				printf("\nSEL %s: ", pAction->object->objName.objName);
				if(IS_NACK(pAction->action->type)){
					printf("fail\n");
				}else{
					printf("success\n");
				}
				break;
			case CMS_MSG_ACT_SETPARA:
				printf("\nSET %s: ", pAction->parameter->parameterName);
				if(IS_NACK(pAction->action->type)){
					printf("fail\n");
				}else{
					printf("success\n");
				}
				break;
			case CMS_MSG_ACT_GETPARA:
				printf("\nGET %s ", pAction->parameter->parameterName);
				if(IS_NACK(pAction->action->type)){
					printf("fail\n");
				}else{
					printf("success\n");
				}

				offsetValue = pAction->paraValue->value;
				if(offsetValue == NULL){
					printf("cms paramter value offset is NULL.\n");
					cms_msgFree(pRecvMsg);
					return -1;
				}
				
				offset = *(cmsoffset_t *)offsetValue;
				getValue = cmsXmlGetObjRealAddr(xmlShmCb, offset);
				if((pAction->paraValue->typeSizeOfValue & PARAMETER_TYPE_STRING)==PARAMETER_TYPE_STRING){
					printf("%-8s type=string\n", " ");
					printf("%-8s SizeOfValue=%d\n", " ", (pAction->paraValue->typeSizeOfValue & ~PARAMETER_TYPE_STRING));
					printf("%-8s Value=%s\n", " ", (char*)getValue);
				}
				else if(pAction->paraValue->typeSizeOfValue==PARAMETER_TYPE_BOOLEAN){
					printf("%-8s type=boolean\n", " ");
					printf("%-8s SizeOfValue=%d\n", " ", (pAction->paraValue->typeSizeOfValue & ~PARAMETER_TYPE_EXTENSION));
					printf("%-8s Value=%d\n", " ", *(cmsBoolean*)getValue);
				}
				else if(pAction->paraValue->typeSizeOfValue==PARAMETER_TYPE_UINT8){
					printf("%-8s type=uint8\n", " ");
					printf("%-8s SizeOfValue=%d\n", " ", pAction->paraValue->typeSizeOfValue);
					printf("%-8s Value=%d\n", " ", *(cmsuint8*)getValue);
				}
				else if(pAction->paraValue->typeSizeOfValue==PARAMETER_TYPE_UINT16){
					printf("%-8s type=uint16\n", " ");
					printf("%-8s SizeOfValue=%d\n", " ", pAction->paraValue->typeSizeOfValue);
					printf("%-8s Value=%d\n", " ", *(cmsuint16*)getValue);
				}
				else if(pAction->paraValue->typeSizeOfValue==PARAMETER_TYPE_UINT32){
					printf("%-8s type=uint32\n", " ");
					printf("%-8s SizeOfValue=%d\n", " ", pAction->paraValue->typeSizeOfValue);
					printf("%-8s Value=0x%08x(%u)\n", " ", *(cmsuint32*)getValue,*(cmsuint32*)getValue);
				}
				else if(pAction->paraValue->typeSizeOfValue==PARAMETER_TYPE_UINT64){
					printf("%-8s type=uint64\n", " ");
					printf("%-8s SizeOfValue=%d\n", " ", pAction->paraValue->typeSizeOfValue);
					printf("%-8s Value=0x%016llx(%llu)\n", " ", *(cmsuint64*)getValue,*(cmsuint64*)getValue);
				}
				else if(pAction->paraValue->typeSizeOfValue==PARAMETER_TYPE_SINT7){
					printf("%-8s type=sint7\n", " ");
					printf("%-8s SizeOfValue=%d\n", " ", (pAction->paraValue->typeSizeOfValue & ~(1 << 31)));
					printf("%-8s Value=%d\n", " ", *(cmssint7*)getValue);
				}
				else if(pAction->paraValue->typeSizeOfValue==PARAMETER_TYPE_SINT15){
					printf("%-8s type=sint15\n", " ");
					printf("%-8s SizeOfValue=%d\n", " ", (pAction->paraValue->typeSizeOfValue & ~(1 << 31)));
					printf("%-8s Value=%d\n", " ", *(cmssint15*)getValue);
				}
				else if(pAction->paraValue->typeSizeOfValue==PARAMETER_TYPE_SINT31){
					printf("%-8s type=sint31\n", " ");
					printf("%-8s SizeOfValue=%d\n", " ", (pAction->paraValue->typeSizeOfValue & ~(1 << 31)));
					printf("%-8s Value=0x%08x(%d)\n", " ", *(cmssint31*)getValue,*(cmssint31*)getValue);
				}
				else if(pAction->paraValue->typeSizeOfValue==PARAMETER_TYPE_SINT63){
					printf("%-8s type=sint63\n", " ");
					printf("%-8s SizeOfValue=%d\n", " ", (pAction->paraValue->typeSizeOfValue & ~(1 << 31)));
					printf("%-8s Value=0x%016llx(%lld)\n", " ", *(cmssint63*)getValue,*(cmssint63*)getValue);
				}
				else if(pAction->paraValue->typeSizeOfValue==PARAMETER_TYPE_MAC){
					printf("%-8s type=mac\n", " ");
					printf("%-8s SizeOfValue=%d\n", " ", pAction->paraValue->typeSizeOfValue);
					printf("%-8s Value=%02x:%02x:%02x:%02x:%02x:%02x\n", " ", *(cmsuint8*)getValue,*((cmsuint8*)getValue+1), *((cmsuint8*)getValue+2),*((cmsuint8*)getValue+3),*((cmsuint8*)getValue+4),*((cmsuint8*)getValue+5));
				}
				else if(pAction->paraValue->typeSizeOfValue==PARAMETER_TYPE_DATETIME){
					printf("%-8s type=datetime\n", " ");
					printf("%-8s SizeOfValue=%d\n", " ", pAction->paraValue->typeSizeOfValue);
					printf("%-8s Value=%s\n", " ", (char*)getValue);
				}
				break;
			case CMS_MSG_ACT_ADDOBJBYPATH:
				if(IS_NACK(pAction->action->type)){
					printf("Add fail\n");
				}else{
					printf("Add success\n");
				}
				break;
			case CMS_MSG_ACT_DELOBJBYPATH:
				if(IS_NACK(pAction->action->type)){
					printf("Del fail\n");
				}else{
					printf("Del success\n");
				}
				break;
			case CMS_MSG_ACT_UPDATE:
				if(IS_NACK(pAction->action->type)){
					printf("stats fail\n");
				}else{
					printf("stats success\n");
				}
				break;
		}
		actCount --;
	}
	cms_msgFree(pRecvMsg);
	pRecvMsg = NULL;
	return ret;
}

static void usage_print(void)
{
	printf("cmc useage:\n");
	printf("1.ADD OBJ:\n");
	printf("%-4s cmc -a InternetGatewayDevice.LANDevice.2\n","");
	printf("2.DEL OBJ:\n");
	printf("%-4s cmc -d InternetGatewayDevice.LANDevice.2\n", "");
	printf("3.SET OBJ para:\n");
	printf("%-4s cmc -s InternetGatewayDevice.LANDevice.2 para1 valuetype value<set paramter value>\n", "");
	printf("%-4s cmc -s InternetGatewayDevice.LANDevice.2 notifyAttr active/passive/off<set object parameters notifyattr>\n", "");
	printf("%-4s cmc -s InternetGatewayDevice.LANDevice.2 para1 notifyAttr active/passive/off<set parameter notifyattr>\n", "");
	printf("4.GET OBJ para:\n");
	printf("%-4s cmc -g InternetGatewayDevice.LANDevice.2<get object paramters value> \n", "");
	printf("%-4s cmc -g InternetGatewayDevice.LANDevice.2 para1<get paramter value>\n", "");
	printf("%-4s cmc -g InternetGatewayDevice.LANDevice.2 notifyAttr<get object paramters notifyAttr>\n", "");
	printf("%-4s cmc -g InternetGatewayDevice.LANDevice.2 para1 notifyAttr<get paramter notify>\n", "");
	printf("5.SEND EVENT:\n");
	printf("%-4s cmc -e eventid eventContentType eventContent\n", "");
	printf("6.UPDATE OBJ:\n");
	printf("%-4s cmc -u InternetGatewayDevice.LANDevice.2\n", "");
	printf("7.SAVE XML:\n");
	printf("%-4s cmc -t save\n", "");
	printf("8.CHANGE cms process Log Level:\n");
	printf("%-4s cmc -l 5 cms_task -- set log level(5-debug,4-info,3-notice,2-warning, 1-error) for running process\n", "");
	printf("9.Configuration file encryption and decrytion:\n");
	printf("%-4s cmc -c e file<encrypt file put in /tmp/cmcEnc.bin>\n", "");
	printf("%-4s cmc -c d file<dencrypt file put in /tmp/cmcDenc.xml>\n", "");
	printf("%-4s cmc -c l  InternetGatewayDevice<get all current object child object name and object id>\n", "");
	printf("%-4s cmc -c c  InternetGatewayDevice 1<restore object to default xml value, if 2, restore value to sysdef define value>\n", "");
	printf("10.print apps and corresponding infomation\n");
	printf("%-4s cmc -p event<print ALL apps and events it subscribed>\n", "");
	printf("%-4s cmc -p event appName<print the app events it subscribed>\n", "");
	printf("%-4s cmc -p object xxxx<xxxx is object full path, print object one layer child info>\n", "");
	printf("%-4s cmc -p objectid all<print all objectid and object path >\n", "");
	printf("%-4s cmc -p objectid xxxx<xxxx is objectid(0,1,2,...), objectid and object path>\n", "");
	printf("%-4s cmc -p showShmSize<show used shm size(sfu max 1M, hgu max 3M)>\n", "");
}

static int cmc_Init(void)
{
	snprintf(moduleName, sizeof(moduleName), "%d%s", getpid(), "cmc");
	cms_registerModule(moduleName);

	xmlShmCb = XMLSHMINIT();
	if(xmlShmCb == NULL){
		printf("no available share memory control block !!\n");
		cms_unregisterModule(moduleName);
		return -1;
	}

	return 0;
}

static void cmc_Deinit(void)
{
	XMLSHMDEINIT(xmlShmCb);
	cms_unregisterModule(moduleName);
}

static int cmc_addObj(char *objName)
{
	cmsMsgCb_t *pReqMsg = NULL;
	int dbMsgId = 0;

	if(objName == NULL){
		CMS_LOG(LL_ERROR, "add obj fail, objname is NULL\n");
		return -1;
	}	

	pReqMsg = cms_createRequestMsg(0);
	if(pReqMsg== NULL){
		return -1;
	} 

	//cms_requestSelObjByName(pReqMsg,objName);
	cms_requestAddObjectByPath(pReqMsg,objName);

	dbMsgId = cms_getModuleIdByName(CMS_DB);
	int ret = cms_SendRecvMsgResult(pReqMsg,dbMsgId,500);
	if(ret == CMS_NOK){
		printf("send msg fail !!\n");
		cms_msgAndShmFree(pReqMsg);
		return -1;
	}

	cms_msgFree(pReqMsg);
	if(ret == CMS_OK){
		printf("add object %s success.\n", objName);
	}else{
		printf("receving cmsg fail !!\n");
		return -1;
	}	
	
	return 0;		
}

static int cmc_delObj(char *objName)
{
	cmsMsgCb_t *pReqMsg = NULL;
	int dbMsgId = 0;

	if(objName == NULL){
		CMS_LOG(LL_ERROR, "add obj fail, objname is NULL\n");
		return -1;
	}	

	pReqMsg = cms_createRequestMsg(0);
	if(pReqMsg== NULL){
		return -1;
	} 

	//cms_requestSelObjByName(pReqMsg,objName);
	cms_requestDelObjectByPath(pReqMsg,objName);

	dbMsgId = cms_getModuleIdByName(CMS_DB);
	int ret = cms_SendRecvMsgResult(pReqMsg,dbMsgId,500);
	if(ret == CMS_NOK){
		printf("send msg fail !!\n");
		cms_msgAndShmFree(pReqMsg);
		return -1;
	}

	cms_msgFree(pReqMsg);
	if(ret == CMS_OK){
		printf("del object %s success.\n", objName);
	}else{
		printf("receving cmsg fail !!\n");
		return -1;
	}	
	
	return 0;		
}

static int cmc_getObjIDIAByName( cmsXmlCb_t *xmlShmCb, char *name, uint32_t *objID, uint64_t *objIA)
{
	void *objPtr = NULL;
	char *strPtr = NULL;
	char tmpStr[ BUFLEN_256 ] = { 0 }, newStr[ BUFLEN_256 ] = { 0 };
	int tmpNum = 0, depth = 0;
	uint64_t targetIA = 0;
	char *Ptr=NULL;

	//printf( "name=%s\n", name );
	memcpy( tmpStr, name, strlen( name ) );
	strPtr = strtok_r( tmpStr, ".", &Ptr );
	while( strPtr )
	{
		tmpNum = atoi( strPtr );
		if ( ( tmpNum > 0 ) && ( tmpNum < 256 ) ) /* find index, increase depth and set IA value with index. */
		{
			depth++;
			targetIA = cmsXmlSetIAValue( targetIA, depth, tmpNum );
			strcat( newStr, "i" );
		}
		else
		{
			strcat( newStr, strPtr );
		}

		strPtr = strtok_r( NULL, ".",&Ptr );
		if ( strPtr != NULL )
		{
			strcat( newStr, "." );
		}
	}
	targetIA = cmsXmlSetIAValue( targetIA, IA_DEPTHNUM, depth );

	/* use new string without index to get prototype object. */
	objPtr = cmsXml_getObjectByProtoName( xmlShmCb, newStr);
	if ( objPtr != NULL )
	{
		*objID =  cmsXml_GetOIDByOffset( xmlShmCb, cmsXmlGetPtrOffset( xmlShmCb, objPtr) );
		*objIA = targetIA;
	}
	else
	{
		return -1;
	}

	return 0;
}

static int cmc_updateObj(char *objName)
{
	uint32_t objID =0;
	uint64_t objIA = 0;
	int ret = -1;

	if(objName == NULL)
		return -1;
	
	ret = cmc_getObjIDIAByName(xmlShmCb, objName, &objID, &objIA);
	if(ret  == -1)
		return ret;

	//printf("OID: %d, OIA: %"PRIx64", %"PRIx64"\n", objID, objIA,  cmsXmlConstructIAValue(1,"2"));
	
	ret = cmsXmlObjectStats(objID, objIA, 300);
	if(ret != 0){
		printf("stats fail.\n");
	}else{
		printf("stats success.\n");
	}

	return ret;
}

static int cmc_sendEvent(char *event, char *typeOfContent, char *eventContent)
{
	long longValue = 0;
	unsigned long ulongValue = 0;
	cmsuint32 eventId;
	cmsuint64 uint64val;
	cmsuint32 uint32val;
	cmsuint16 uint16val;
	cmsuint8 uint8val;
	cmsBoolean boolval;		/* True / False */
	cmssint7 sint7val;
	cmssint15 sint15val;
	cmssint31 sint31val;
	cmssint63 sint63val;
	uint32_t type_size = 0;
	int ret = -1;
	
	eventId = strtoul(event, NULL, 0);

	if((typeOfContent == NULL) || (eventContent == NULL)){
		ret = cms_eventNotify(eventId, PARAMETER_TYPE_STRING, NULL);
		return 0;
	}

	if(!(strcmp(typeOfContent, "boolean"))){
		ulongValue = strtoul(eventContent, NULL, 0);
		if((ulongValue != 0)&&(ulongValue != 1)){
			printf("%lu out of range of boolean !!\n", ulongValue);
			return -1;
		}
		type_size = PARAMETER_TYPE_BOOLEAN;
		boolval = (cmsBoolean)ulongValue;
		ret = cms_eventNotify(eventId,type_size,&boolval);
	}
	else if(!(strcmp(typeOfContent, "uint8"))){
		ulongValue = strtoul(eventContent, NULL, 0);
		if((ulongValue < 0)||(ulongValue > 255)){
			printf("%lu out of range of uint8 !!\n", ulongValue);
			return -1;
		}
		type_size = PARAMETER_TYPE_UINT8;
		uint8val = (cmsBoolean)ulongValue;
		ret = cms_eventNotify(eventId,type_size,&uint8val);	
	}
	else if(!(strcmp(typeOfContent, "uint16"))){
		ulongValue = strtoul(eventContent, NULL, 0);
		if((ulongValue < 0)||(ulongValue > 65535)){
			printf("%lu out of range of uint16 !!\n", ulongValue);
			return -1;
		}
		type_size = PARAMETER_TYPE_UINT16;
		uint16val = (cmsuint16)ulongValue;
		ret = cms_eventNotify(eventId,type_size,&uint16val); 
	}
	else if(!(strcmp(typeOfContent, "uint32"))){
		type_size = PARAMETER_TYPE_UINT32;
		uint32val = strtoul(eventContent, NULL, 0);;
		ret = cms_eventNotify(eventId,type_size,&uint32val);  
	}
	else if(!(strcmp(typeOfContent, "uint64"))){
		type_size = PARAMETER_TYPE_UINT64;
		uint64val = strtoull(eventContent, NULL, 0);;
		ret = cms_eventNotify(eventId,type_size,&uint64val); 
	}
	else if(!(strcmp(typeOfContent, "sint7"))){
		longValue = strtol(eventContent, NULL, 0);
		if((longValue < -128)||(longValue > 127)){
			printf("Line %ld: out of range of sint7 !!\n", longValue);
			return -1;
		}
		type_size = PARAMETER_TYPE_SINT7;
		sint7val = (cmssint7)longValue;
		ret = cms_eventNotify(eventId,type_size,&sint7val);	
	}
	else if(!(strcmp(typeOfContent, "sint15"))){
		longValue = strtol(eventContent, NULL, 0);
		if((longValue < -32768)||(longValue > 32767)){
			printf("%ld out of range of sint15 !!\n", longValue);
			return -1;
		}
		type_size = PARAMETER_TYPE_SINT15;
		sint15val = (cmsBoolean)longValue;
		ret = cms_eventNotify(eventId,type_size,&sint15val); 
	}
	else if(!(strcmp(typeOfContent, "sint31"))){
		type_size = PARAMETER_TYPE_SINT31;
		sint31val = strtol(eventContent, NULL, 0);;
		ret = cms_eventNotify(eventId,type_size,&sint31val);
	}
	else if(!(strcmp(typeOfContent, "sint63"))){
		type_size = PARAMETER_TYPE_SINT63;
		sint63val = strtoll(eventContent, NULL, 0);;
		ret = cms_eventNotify(eventId,type_size,&sint63val);
	}
	else if(!(strcmp(typeOfContent,"string"))){
		type_size = PARAMETER_TYPE_STRING;
		ret = cms_eventNotify(eventId,type_size,eventContent ? eventContent: "");
	}else
		CMS_LOG(LL_ERROR, "not support value type %s\n", typeOfContent);

	return ret;
		
}

static int cmc_getParameter(char *objName, char *parameterName)
{
	void *objPtr = NULL;
	void **objParameters = NULL;
	cmsMsgCb_t *pReqMsg = NULL, *pRecvMsg = NULL;
	uint32_t dbId = 0;
	int i = 0;

	if(objName == NULL){
		CMS_LOG(LL_ERROR, "The obj name is NULL.\n");
		return -1;
	}

	objPtr = cmsXmlGetObjectByName(xmlShmCb, objName);
	if(objPtr == NULL){
		CMS_LOG(LL_ERROR, "get object %s fail.\n", objName);
		return -1;
	}
	
	pReqMsg = cms_createRequestMsg(0);
	if(pReqMsg== NULL){
		CMS_LOG(LL_ERROR, "create request msg fail.\n");
		return -1;
	}

	cms_requestSelObjByName(pReqMsg,objName);

	if(parameterName != NULL)
		cms_requestGetPara(pReqMsg,parameterName);
	else{
	
		objParameters = cmsXml_GetParameterNameByObjectName(xmlShmCb, objName);
		if(objParameters){
			for(i = 0; i < xmlShmCb->meta_count; i++){
				cms_requestGetPara(pReqMsg,*(objParameters+i));
			}
		}else{
			CMS_LOG(LL_ERROR, "get object parameter names fail.\n");
			cms_msgFree(pReqMsg);
			return -1;
		}
	
	}

	dbId = cms_getModuleIdByName(CMS_DB);
	pRecvMsg = cms_SendRecvMsg(pReqMsg,dbId,500);
	cms_msgFree(pReqMsg);
	if(pRecvMsg != NULL){
		parseRcvMsg(pRecvMsg, xmlShmCb);
	}else{
		printf("receving cmsg fail !!\n");
		return -1;
	}

	return 0;
}

static int cmc_setParameter(char *objName, char *parameter, char *valueType, char *valueBuf)
{
	cmsMsgCb_t *pReqMsg = NULL;
	uint32_t dbId = 0;
	long longValue = 0;
	unsigned long ulongValue = 0;
	cmsuint64 uint64val;
	cmsuint32 uint32val;
	cmsuint16 uint16val;
	cmsuint8 uint8val;
	cmsBoolean boolval;		/* True / False */
	cmssint7 sint7val;
	cmssint15 sint15val;
	cmssint31 sint31val;
	cmssint63 sint63val;
	uint32_t type_size = 0;

	if((objName == NULL) || (parameter == NULL))
		return -1;

	pReqMsg = cms_createRequestMsg(0);
	if(pReqMsg== NULL){
		return -1;
	}

	cms_requestSelObjByName(pReqMsg,objName);


	if(!(strcmp(valueType, "boolean"))){
		ulongValue = strtoul(valueBuf, NULL, 0);
		if((ulongValue != 0)&&(ulongValue != 1)){
			printf("%lu: out of range of boolean !!\n", ulongValue);
			cms_msgFree(pReqMsg);
			return -1;
		}
		type_size = PARAMETER_TYPE_BOOLEAN;
		boolval = (cmsBoolean)ulongValue;
		cms_requestSetPara(pReqMsg,parameter,type_size,&boolval);
	}
	else if(!(strcmp(valueType, "uint8"))){
		ulongValue = strtoul(valueBuf, NULL, 0);
		if((ulongValue < 0)||(ulongValue > 255)){
			printf("%lu: out of range of uint8 !!\n", ulongValue);
			cms_msgFree(pReqMsg);
			return -1;
		}
		type_size = PARAMETER_TYPE_UINT8;
		uint8val = (cmsBoolean)ulongValue;
		cms_requestSetPara(pReqMsg,parameter,type_size,&uint8val);	
	}
	else if(!(strcmp(valueType, "uint16"))){
		ulongValue = strtoul(valueBuf, NULL, 0);
		if((ulongValue < 0)||(ulongValue > 65535)){
			printf("%lu: out of range of uint16 !!\n", ulongValue);
			cms_msgFree(pReqMsg);
			return -1;
		}
		type_size = PARAMETER_TYPE_UINT16;
		uint16val = (cmsuint16)ulongValue;
		cms_requestSetPara(pReqMsg,parameter,type_size,&uint16val);	
	}
	else if(!(strcmp(valueType, "uint32"))){
		type_size = PARAMETER_TYPE_UINT32;
		uint32val = strtoul(valueBuf, NULL, 0);;
		cms_requestSetPara(pReqMsg,parameter,type_size,&uint32val);	
	}
	else if(!(strcmp(valueType, "uint64"))){
		type_size = PARAMETER_TYPE_UINT64;
		uint64val = strtoull(valueBuf, NULL, 0);;
		cms_requestSetPara(pReqMsg,parameter,type_size,&uint64val);	
	}
	else if(!(strcmp(valueType, "sint7"))){
		longValue = strtol(valueBuf, NULL, 0);
		if((longValue < -128)||(longValue > 127)){
			printf("%ld: out of range of sint7 !!\n", longValue);
			cms_msgFree(pReqMsg);
			return -1;
		}
		type_size = PARAMETER_TYPE_SINT7;
		sint7val = (cmssint7)longValue;
		cms_requestSetPara(pReqMsg,parameter,type_size,&sint7val);	
	}
	else if(!(strcmp(valueType, "sint15"))){
		longValue = strtol(valueBuf, NULL, 0);
		if((longValue < -32768)||(longValue > 32767)){
			printf("%ld: out of range of sint15 !!\n", longValue);
			cms_msgFree(pReqMsg);
			return -1;
		}
		type_size = PARAMETER_TYPE_SINT15;
		sint15val = (cmssint15)longValue;
		cms_requestSetPara(pReqMsg,parameter,type_size,&sint15val);	
	}
	else if(!(strcmp(valueType, "sint31"))){
		type_size = PARAMETER_TYPE_SINT31;
		sint31val = strtol(valueBuf, NULL, 0);;
		cms_requestSetPara(pReqMsg,parameter,type_size,&sint31val);	
	}
	else if(!(strcmp(valueType, "sint63"))){
		type_size = PARAMETER_TYPE_SINT63;
		sint63val = strtoll(valueBuf, NULL, 0);;
		cms_requestSetPara(pReqMsg,parameter,type_size,&sint63val);	
	}
	else if(!(strcmp(valueType,"string"))){
		type_size = PARAMETER_TYPE_STRING;
		cms_requestSetPara(pReqMsg,parameter,type_size,valueBuf ? valueBuf: "");
	}
	else if(!(strcmp(valueType,"mac"))){
		type_size = PARAMETER_TYPE_MAC;
		cms_requestSetPara(pReqMsg,parameter,type_size,valueBuf ? valueBuf: "");
	}
	else if(!(strcmp(valueType,"datetime"))){
		type_size = PARAMETER_TYPE_DATETIME;
		cms_requestSetPara(pReqMsg,parameter,type_size,valueBuf ? valueBuf: "");
	}else{
		printf("no this value type <%s> !!\n", valueType);
		cms_msgFree(pReqMsg);
		return -1;
	}

	dbId = cms_getModuleIdByName(CMS_DB);
	int ret = cms_SendRecvMsgResult(pReqMsg,dbId,500);
	if(ret == CMS_NOK){
		printf("send msg fail !!\n");
		cms_msgAndShmFree(pReqMsg);
		return -1;
	}

	cms_msgFree(pReqMsg);
	if(ret == CMS_OK){
		printf("set object( %s) param(%s) success.\n", objName,parameter);
	}else{
		printf("receving cmsg fail !!\n");
		return -1;
	}	

	return 0;
}


static int cmc_handleByFile(char *fileName)
{
	uint32_t type_size = 0;
	char* delim = " ;\n";
	char* delim2 = ";\n";
	int dbId;
	FILE* fp;
	char buffer[1024] = {0};
	int iLine = 0;
	char* action;
	char* parameter;
	char* objName;
	char* valueType;
	char* valueBuf;
	char* eventIdBuf;
	cmsMsgCb_t *pReqMsg = NULL, *pRecvMsg = NULL;
	long longValue = 0;
	unsigned long ulongValue = 0;
	cmsuint32 eventId;
	cmsuint64 uint64val;
	cmsuint32 uint32val;
	cmsuint16 uint16val;
	cmsuint8 uint8val;
	cmsBoolean boolval;		/* True / False */
	cmssint7 sint7val;
	cmssint15 sint15val;
	cmssint31 sint31val;
	cmssint63 sint63val;
	char *Ptr=NULL;

	dbId = cms_getModuleIdByName(CMS_DB);

	if((fp = fopen(fileName, "r")) == NULL){
		printf("fopen fail !!\n");
		return -1;
	}

	while(fgets(buffer, 1024, fp)){
		iLine++;
		if((action = strtok_r(buffer, delim, &Ptr))== NULL){
			printf("error\n");
			break;
		}

		if(!strcmp(action, "SEL")){
			pReqMsg = cms_createRequestMsg(0);
			if(pReqMsg== NULL){
				return -1;
			}
			
			objName = strtok_r(NULL, delim, &Ptr);
			printf("SEL objname:%s\n", objName);
			cms_requestSelObjByName(pReqMsg,objName);
			continue;
		}
		if(!strcmp(action, "SET")){
			parameter = strtok_r(NULL, delim, &Ptr);
			valueType = strtok_r(NULL, delim, &Ptr);
			valueBuf = strtok_r(NULL, delim2, &Ptr);
			printf("SET parameter:%s, vlaueType:%s, valueBuf:%s\n", parameter, valueType, valueBuf);

			if(!(strcmp(valueType, "boolean"))){
				ulongValue = strtoul(valueBuf, NULL, 0);
				if((ulongValue != 0)&&(ulongValue != 1)){
					printf("Line %d: out of range of boolean !!\n", iLine);
					cms_msgFree(pReqMsg);
					return -1;
				}
				type_size = PARAMETER_TYPE_BOOLEAN;
				boolval = (cmsBoolean)ulongValue;
				cms_requestSetPara(pReqMsg,parameter,type_size,&boolval);
			}
			else if(!(strcmp(valueType, "uint8"))){
				ulongValue = strtoul(valueBuf, NULL, 0);
				if((ulongValue < 0)||(ulongValue > 255)){
					printf("Line %d: out of range of uint8 !!\n", iLine);
					cms_msgFree(pReqMsg);
					return -1;
				}
				type_size = PARAMETER_TYPE_UINT8;
				uint8val = (cmsBoolean)ulongValue;
				cms_requestSetPara(pReqMsg,parameter,type_size,&uint8val);	
			}
			else if(!(strcmp(valueType, "uint16"))){
				ulongValue = strtoul(valueBuf, NULL, 0);
				if((ulongValue < 0)||(ulongValue > 65535)){
					printf("Line %d: out of range of uint16 !!\n", iLine);
					cms_msgFree(pReqMsg);
					return -1;
				}
				type_size = PARAMETER_TYPE_UINT16;
				uint16val = (cmsuint16)ulongValue;
				cms_requestSetPara(pReqMsg,parameter,type_size,&uint16val);	
			}
			else if(!(strcmp(valueType, "uint32"))){
				type_size = PARAMETER_TYPE_UINT32;
				uint32val = strtoul(valueBuf, NULL, 0);;
				cms_requestSetPara(pReqMsg,parameter,type_size,&uint32val);	
			}
			else if(!(strcmp(valueType, "uint64"))){
				type_size = PARAMETER_TYPE_UINT64;
				uint64val = strtoull(valueBuf, NULL, 0);;
				cms_requestSetPara(pReqMsg,parameter,type_size,&uint64val);	
			}
			else if(!(strcmp(valueType, "sint7"))){
				longValue = strtol(valueBuf, NULL, 0);
				if((longValue < -128)||(longValue > 127)){
					printf("Line %d: out of range of sint7 !!\n", iLine);
					cms_msgFree(pReqMsg);
					return -1;
				}
				type_size = PARAMETER_TYPE_SINT7;
				sint7val = (cmssint7)longValue;
				cms_requestSetPara(pReqMsg,parameter,type_size,&sint7val);	
			}
			else if(!(strcmp(valueType, "sint15"))){
				longValue = strtol(valueBuf, NULL, 0);
				if((longValue < -32768)||(longValue > 32767)){
					printf("Line %d: out of range of sint15 !!\n", iLine);
					cms_msgFree(pReqMsg);
					return -1;
				}
				type_size = PARAMETER_TYPE_SINT15;
				sint15val = (cmsBoolean)longValue;
				cms_requestSetPara(pReqMsg,parameter,type_size,&sint15val);	
			}
			else if(!(strcmp(valueType, "sint31"))){
				type_size = PARAMETER_TYPE_SINT31;
				sint31val = strtol(valueBuf, NULL, 0);;
				cms_requestSetPara(pReqMsg,parameter,type_size,&sint31val);	
			}
			else if(!(strcmp(valueType, "sint63"))){
				type_size = PARAMETER_TYPE_SINT63;
				sint63val = strtoll(valueBuf, NULL, 0);;
				cms_requestSetPara(pReqMsg,parameter,type_size,&sint63val);	
			}
			else if(!(strcmp(valueType,"string"))){
				type_size = PARAMETER_TYPE_STRING;
				cms_requestSetPara(pReqMsg,parameter,type_size,valueBuf ? valueBuf: "");
			}
			else if(!(strcmp(valueType,"mac"))){
				type_size = PARAMETER_TYPE_MAC;
				cms_requestSetPara(pReqMsg,parameter,type_size,valueBuf ? valueBuf: "");
			}
			else if(!(strcmp(valueType,"datetime"))){
				type_size = PARAMETER_TYPE_DATETIME;
				cms_requestSetPara(pReqMsg,parameter,type_size,valueBuf ? valueBuf: "");
			}else{
				printf("Line %d: no this value type <%s> !!\n", iLine, valueType);
				cms_msgFree(pReqMsg);
				return -1;
			}
			continue;
		}
		if(!strcmp(action, "ADD")){
			pReqMsg = cms_createRequestMsg(0);
			if(pReqMsg== NULL){
				return -1;
			}
			
			objName = strtok_r(NULL, delim, &Ptr);
			//cms_requestSelObjByName(pReqMsg,objName);
			cms_requestAddObjectByPath(pReqMsg,objName);
		}
		if(!strcmp(action, "DELETE")){
			pReqMsg = cms_createRequestMsg(0);
			if(pReqMsg== NULL){
				return -1;
			}
			
			objName = strtok_r(NULL, delim, &Ptr);
			//cms_requestSelObjByName(pReqMsg,objName);
			cms_requestDelObjectByPath(pReqMsg,objName);
		}
		if(!strcmp(action, "GET")){
			parameter = strtok_r(NULL, delim, &Ptr);
			cms_requestGetPara(pReqMsg,parameter);
		}
		if(!strcmp(action, "SAVE")){
			pReqMsg = cms_createRequestMsg(0);
			if(pReqMsg== NULL){
				return -1;
			}
			
			cms_setSaveActionRequest(pReqMsg,xmlShmCb->shmId);
		}
		if(!strcmp(action, "EVENTNOTIFY")){
			pReqMsg = cms_createRequestMsg(0);
			if(pReqMsg== NULL){
				return -1;
			}
			
			eventIdBuf = strtok_r(NULL, delim, &Ptr);
			eventId = strtoul(eventIdBuf, NULL, 0);
			if((valueType = strtok_r(NULL, delim, &Ptr)) != NULL){
				printf("valueType: %s\n", valueType);
				valueBuf = strtok_r(NULL, delim, &Ptr);
				if(!(strcmp(valueType, "boolean"))){
					ulongValue = strtoul(valueBuf, NULL, 0);
					if((ulongValue != 0)&&(ulongValue != 1)){
						printf("Line %d: out of range of boolean !!\n", iLine);
						cms_msgFree(pReqMsg);
						return -1;
					}
					type_size = PARAMETER_TYPE_BOOLEAN;
					boolval = (cmsBoolean)ulongValue;
					cms_requestEventNotify(pReqMsg,eventId,type_size,&boolval);
				}
				else if(!(strcmp(valueType, "uint8"))){
					ulongValue = strtoul(valueBuf, NULL, 0);
					if((ulongValue < 0)||(ulongValue > 255)){
						printf("Line %d: out of range of uint8 !!\n", iLine);
						cms_msgFree(pReqMsg);
						return -1;
					}
					type_size = PARAMETER_TYPE_UINT8;
					uint8val = (cmsBoolean)ulongValue;
					cms_requestEventNotify(pReqMsg,eventId,type_size,&uint8val);	
				}
				else if(!(strcmp(valueType, "uint16"))){
					ulongValue = strtoul(valueBuf, NULL, 0);
					if((ulongValue < 0)||(ulongValue > 65535)){
						printf("Line %d: out of range of uint16 !!\n", iLine);
						cms_msgFree(pReqMsg);
						return -1;
					}
					type_size = PARAMETER_TYPE_UINT16;
					uint16val = (cmsuint16)ulongValue;
					cms_requestEventNotify(pReqMsg,eventId,type_size,&uint16val); 
				}
				else if(!(strcmp(valueType, "uint32"))){
					type_size = PARAMETER_TYPE_UINT32;
					uint32val = strtoul(valueBuf, NULL, 0);;
					cms_requestEventNotify(pReqMsg,eventId,type_size,&uint32val);  
				}
				else if(!(strcmp(valueType, "uint64"))){
					type_size = PARAMETER_TYPE_UINT64;
					uint64val = strtoull(valueBuf, NULL, 0);;
					cms_requestEventNotify(pReqMsg,eventId,type_size,&uint64val); 
				}
				else if(!(strcmp(valueType, "sint7"))){
					longValue = strtol(valueBuf, NULL, 0);
					if((longValue < -128)||(longValue > 127)){
						printf("Line %d: out of range of sint7 !!\n", iLine);
						cms_msgFree(pReqMsg);
						return -1;
					}
					type_size = PARAMETER_TYPE_SINT7;
					sint7val = (cmssint7)longValue;
					cms_requestEventNotify(pReqMsg,eventId,type_size,&sint7val);	
				}
				else if(!(strcmp(valueType, "sint15"))){
					longValue = strtol(valueBuf, NULL, 0);
					if((longValue < -32768)||(longValue > 32767)){
						printf("Line %d: out of range of sint15 !!\n", iLine);
						return -1;
					}
					type_size = PARAMETER_TYPE_SINT15;
					sint15val = (cmsBoolean)longValue;
					cms_requestEventNotify(pReqMsg,eventId,type_size,&sint15val); 
				}
				else if(!(strcmp(valueType, "sint31"))){
					type_size = PARAMETER_TYPE_SINT31;
					sint31val = strtol(valueBuf, NULL, 0);;
					cms_requestEventNotify(pReqMsg,eventId,type_size,&sint31val);
				}
				else if(!(strcmp(valueType, "sint63"))){
					type_size = PARAMETER_TYPE_SINT63;
					sint63val = strtoll(valueBuf, NULL, 0);;
					cms_requestEventNotify(pReqMsg,eventId,type_size,&sint63val);
				}
				else if(!(strcmp(valueType,"string"))){
					type_size = PARAMETER_TYPE_STRING;
					cms_requestEventNotify(pReqMsg,eventId,type_size,valueBuf ? valueBuf: "");
				}
			}else{
				cms_requestEventNotify(pReqMsg,eventId,0,NULL);
			}
			if(cms_sendMsg(pReqMsg,0) == -1){
				printf("cmc send message fail !!\n");
			}
			cms_msgFree(pReqMsg);
			pReqMsg = NULL;
			fclose(fp);
			return 0;
		}
		if(!strcmp(action, "SEND")){
			pRecvMsg = cms_SendRecvMsg(pReqMsg,dbId,60000);
			cms_msgFree(pReqMsg);
			pReqMsg = NULL;
			if(pRecvMsg != NULL){
				parseRcvMsg(pRecvMsg, xmlShmCb);
			}else{
				printf("receving cmsg fail !!\n");
			}
			cms_msgFree(pReqMsg);
			continue;
		}
		if(!strcmp(action, "UPDATE")){
			int ret = -1;
			void *object = NULL;
			objName = strtok_r(NULL, delim, &Ptr);
			printf("update objname:%s\n", objName);

			object = cmsXmlGetObjectByName(xmlShmCb, objName);
			if(object == NULL)
				continue;

			printf("OID: %d, OIA: %"PRIx64"\n", xmlShmCb->OID,xmlShmCb->IA);
			ret = cmsXmlObjectStats(xmlShmCb->OID,xmlShmCb->IA,300);

			if(ret != 0)
				printf("stats fail.\n");
			else
				printf("stats success.\n");
			
			continue;
		}
	}

	fclose(fp);

	return 0;
}

static int cmc_setNotifyAttr(char *objName, char *parameterName, int notifyType)
{
	void *object = NULL;
	void **parameterList = NULL;
	uint32_t objOff = 0;
	uint32_t notifyAttr = 0;
	uint32_t paramAttr = 0;
	int i = 0;

	if(objName == NULL)
		return CMS_NOK;

	if(parameterName == NULL){	
		parameterList = cmsXml_GetParameterNameByObjectName(xmlShmCb,objName);
		if(parameterList != NULL){
			object = cmsXmlGetObjectByName(xmlShmCb, objName);
			if(object){
				objOff = cmsXmlGetPtrOffset(xmlShmCb, object);
				
				for(i = 0; i < xmlShmCb->meta_count; i++){
					if(*(parameterList + i) != NULL){
						paramAttr = cmsXml_getParameterAttr(xmlShmCb,objOff, *(parameterList + i));
						if(!(((paramAttr & PARAMETER_ATTR_ACTIVE_UNSUPPORT) == PARAMETER_ATTR_ACTIVE_UNSUPPORT) &&
							(notifyType == 2))){
							notifyAttr = 0;
							notifyAttr |= PARAMETER_ATTR_NOTIFY;
							cmsXmlSetParameterAttr(xmlShmCb,objOff, *(parameterList + i),notifyAttr,PARAMETER_ATTR_INACTIVE);

							notifyAttr = 0;
							if(notifyType == 2)
								notifyAttr |= PARAMETER_ATTR_ACTIVENOTIFY;
							else if(notifyType == 1)
								notifyAttr |= PARAMETER_ATTR_PASSIVENOTIFY;

							cmsXmlSetParameterAttr(xmlShmCb,objOff, *(parameterList + i),notifyAttr,PARAMETER_ATTR_ACTIVE);
						}
					}
				}
			}

			cmsXml_freeParameterNameBuffer(parameterList);
		}else{
			CMS_LOG(LL_ERROR,"get object parametername fail.\n");
			return CMS_NOK;
		}
	}else{
		
		object = cmsXmlGetObjectByName(xmlShmCb, objName);
		if(object){
			objOff = cmsXmlGetPtrOffset(xmlShmCb, object);
			paramAttr = cmsXml_getParameterAttr(xmlShmCb,objOff, parameterName);
			if(!(((paramAttr & PARAMETER_ATTR_ACTIVE_UNSUPPORT) == PARAMETER_ATTR_ACTIVE_UNSUPPORT) &&
				(notifyType == 2))){
				notifyAttr = 0;
				notifyAttr |= PARAMETER_ATTR_NOTIFY;
				cmsXmlSetParameterAttr(xmlShmCb,objOff, parameterName,notifyAttr,PARAMETER_ATTR_INACTIVE);

				notifyAttr = 0;
				if(notifyType == 2)
					notifyAttr |= PARAMETER_ATTR_ACTIVENOTIFY;
				else if(notifyType == 1)
					notifyAttr |= PARAMETER_ATTR_PASSIVENOTIFY;
							
				cmsXmlSetParameterAttr(xmlShmCb,objOff, parameterName,notifyAttr,PARAMETER_ATTR_ACTIVE);
			}else{
				CMS_LOG(LL_ERROR,"set parameter %s noitify attr fail, this param active notify attr is unsupport!!!\n", parameterName);
				return CMS_NOK;
			}
		}else{
			CMS_LOG(LL_ERROR,"get object %s fail.\n", objName);
			return CMS_NOK;
		}
	}

	return CMS_OK;
}

#if 0
static int cmc_encryptXml(const char *srcXmlFile)
{
	char *destEncFile = "/tmp/cmcEnc.bin";
	FILE *fpIn = NULL, *fpOut = NULL;
	struct stat fileBuf;
	char data[1024] = {0};
	uint32_t len = 0;
	int flag = 0;  
	int i;

	if(srcXmlFile == NULL)
		return -1;

	if (stat(srcXmlFile, &fileBuf) < 0){
			return -1;
	}else{
		if(fileBuf.st_size == 0){
			return -1;
		}else{
			fpIn = fopen(srcXmlFile, "r");
			if(fpIn == NULL)
				return -1;
		}
	}

	fpOut = fopen(destEncFile, "w+");
	if(fpOut == NULL){
		fclose(fpIn);
		return -1;
	}

	while(fgets(data, 1024, fpIn) != NULL){
		
		if(flag == 0){
			if(strstr(data,"xml version") && strstr(data, "encoding") && strstr(data, "utf-8")){
				CMS_LOG(LL_DEBUG, "check ok\n");
				flag = 1;
			}else{
				fclose(fpOut);
				fclose(fpIn);
				return -1;
			}
		}

		len = strlen(data);
		
		for(i = 0; i < len; i++){
			data[i] = data[i] ^ ENCRYKEY;
		}

		if(data[len-1] == ',')
			data[len-1] = '\0';

		fputs(data, fpOut);
		fputs("\n", fpOut);

		memset(data, 0, sizeof(data));
	}

	fclose(fpOut);
	fclose(fpIn);

	return 0;
}

static int cmc_decryptXml(char *srcBinFile)
{
	FILE *fpIn = NULL, *fpOut = NULL;
	char data[1024] = {0};
	char tmpData[1024] = {0};
	uint32_t len = 0;
	int flag1 = 0, flag2 = 0;
	int i = 0, j =0;

	if(srcBinFile == NULL){
		CMS_LOG(LL_ERROR, "xmlPath is NULL.\n");
		return -1;
	}
	
	fpIn = fopen(srcBinFile, "r");
	if(fpIn == NULL){
		CMS_LOG(LL_ERROR, "xmlPath can't open, %s.\n", srcBinFile);
		return -1;
	}
	

	fpOut = fopen("/tmp/cmcDenc.xml", "w+");
	if(fpOut == NULL){
		CMS_LOG(LL_ERROR, "xmlout can't open.\n");
		fclose(fpIn);
		return -1;
	}
	

	while(fgets(data, 1024, fpIn) != NULL){
		if(flag1 == 0){
			if(strstr(data,"xml version") && strstr(data, "encoding") && strstr(data, "utf-8")){
				CMS_LOG(LL_DEBUG, "check fail\n");
				fclose(fpOut);
				fclose(fpIn);
				return -1;
			}else{
				CMS_LOG(LL_DEBUG, "check ok\n");
				flag1 = 1;
			}
		}
		
		len = strlen(data);

		/*remove \r from string to compatible with  windows*/
		if(data[len-2] == '\r'){
			j = 0;
			for(i = 0; i < len; i++){
				if(i != (len -2))
					tmpData[j++] = data[i]; 
			}
			memset(data, 0, sizeof(data));
			strcpy(data, tmpData);
			len = strlen(data);
		}
		
		
		for(i = 0; i < len; i++){
			data[i] = data[i] ^ ENCRYKEY;
		}
		
		if(flag2 == 0){
			if(strstr(data,"xml version") && strstr(data, "encoding") && strstr(data, "utf-8")){
				CMS_LOG(LL_DEBUG, "check ok\n");
				flag2 = 1;
			}else{
				fclose(fpOut);
				fclose(fpIn);
				return -1;
			}
		}

		if((data[len-1] == ',') && (data[len-2] == '>')){
			strncpy(tmpData, data, len-1);
			fputs(tmpData, fpOut);
			fputs("\n", fpOut);
		}else{
			strncpy(tmpData, data, len);
			fputs(tmpData, fpOut);
		}

		memset(data, 0, sizeof(data));
		memset(tmpData, 0, sizeof(tmpData));
	}

	fclose(fpOut);
	fclose(fpIn);

	return 0;
}
#endif

/*-1 - pathname error, 0 - get fail, 1 - get success*/
static int cmc_getChildList(char *objName, cmsBoolean isGetALL)
{
	void *object = NULL;
	objectList_t *objChildList = NULL, *tmpChildList = NULL;
	char *childObjName = NULL;
	uint32_t objID = 0;
	uint64_t objIA = 0;
	int ret = -1;
	
	if(objName == NULL){
		CMS_LOG(LL_ERROR, "object path name is NULL!!!\n");
		return -1;
	}

	object = cmsXmlGetObjectByName(xmlShmCb, objName);
	if(object == NULL){
		CMS_LOG(LL_ERROR, "get object %s fail.\n", objName);
		return 0;
	}

	ret = cmc_getObjIDIAByName(xmlShmCb, objName, &objID, &objIA);
	if(ret == -1){
		CMS_LOG(LL_ERROR, "get obj id & ia by name fail.\n");
		return 0;
	}

	if(isGetALL == TRUE)
		objChildList = cmsXml_GetFullChildList(xmlShmCb, objID, objIA);
	else
		objChildList = cmsXml_GetOneLayerChildList(xmlShmCb, objID, objIA);
	tmpChildList = objChildList;

	while(tmpChildList){
		childObjName = cmsXml_GetFullpathByOIDIA(tmpChildList->OID, tmpChildList->IA);
		CMS_LOG(LL_ERROR, "object pathName: %s, id: %d, IA: %"PRIx64".\n", ((childObjName == NULL) ? "get child name fail" : childObjName), tmpChildList->OID, tmpChildList->IA);
		cmsXml_freeFullPathBuffer((void *)&childObjName);
		tmpChildList = tmpChildList->next;
	}

	if(objChildList)
		cmsXml_FreeObjList(&objChildList);

	return 1;
	
}

static int cmc_printEventInfo(char *moduleName)
{
	cmsMsgCb_t *pReqMsg = NULL, *pRecvMsg = NULL;
	int msqId = -1;
	int key = -1;
	int ret = CMS_NOK;
	
	pReqMsg = cms_createRequestMsg(0);
	if(pReqMsg == NULL){
		CMS_LOG(LL_ERROR, "create request msg fail.\n");
		return ret;
	}

	if(moduleName == NULL)
		cms_requestMsg(pReqMsg,CMS_MSG_ACT_GET_ALL_EVENT);
	else
		cms_requestGetAppEvent(pReqMsg, moduleName);
		
	genKeyByName(CMS_CORE, &key, NULL);
	if((msqId = msgget(key, 0666)) == -1){
		cms_msgFree(pReqMsg);
		CMS_LOG(LL_ERROR, "get dst module msqId failed, key 0x%x.\n", key);
		return ret;
	}

	pRecvMsg = cms_SendRecvMsg(pReqMsg,msqId,3000);
	cms_msgFree(pReqMsg);
	if(pRecvMsg)
		cms_msgFree(pRecvMsg);

	return ret;
}

//get cms object-id full path by objId 
static int cmc_getObjectIdPath(int OID)
{
	char *objPathName = NULL;
	
	objPathName = cmsXml_GetFullPathByOID(OID);
	CMS_LOG(LL_ERROR, "%-4d : %s", OID, objPathName);
	cmsXml_freeFullPathBuffer((void **)&objPathName);
	
	return 0;
}

//get cms object-id full path in libMdm.so  
static int cmc_getAllObjectIdPath(void)
{
	int oid;
	int oidMax;
	
	oidMax = cmsXml_GetOIDMax();
	for(oid=0; oid<=oidMax; oid++){
		cmc_getObjectIdPath(oid);
	}
	
	return 0;
}

int main(int argc, char* argv[]){
	char processName[24] = {0};
	int recordFlag = 0;
	int notifyType = 0;
	int cmsloglevel = LL_ERROR;
	int opt;
	
	cmc_Init();
	set_log_level("cmc", LL_ERROR); 
	
	while((opt = getopt(argc, argv, "a:d:s:g:e:u:t:f:l:c:p:h:")) != -1){
		switch(opt){
		case 'a':
			if(argc != 3){
				goto error;
			}
			recordFlag = 1;
			
			cmc_addObj(argv[2]);
			
			break;
		case 'd':
			if(argc != 3){
				goto error;
			}
			
			recordFlag = 1;
			
			cmc_delObj(argv[2]);
			
			break;
		case 's':
			if(argc != 5 && argc != 6){
				goto error;
			}
			recordFlag = 1;

			if(argc == 5){//set objecat all paramter notify attr
				if(strcasecmp(argv[3], "notifyAttr")){
					goto error;
				}

				if(!strcasecmp(argv[4], "off")){
					notifyType = 0;
				}else if(!strcasecmp(argv[4], "active")){
					notifyType = 2;
				}else if(!strcasecmp(argv[4], "passive")){
					notifyType = 1;
				}else{
					CMS_LOG(LL_ERROR, "notifyAttr is wrong!!\n");
					goto error;
				}

				if(cmc_setNotifyAttr(argv[2], NULL, notifyType) != CMS_OK){
					printf("set object %s paramters notify attr(%s) fail.\n", argv[2], argv[4]);
					goto exit;
				}else{
					printf("set object %s paramters notify attr(%s) success.\n", argv[2], argv[4]);
				}
			}else if(argc == 6){
				if(!strcasecmp(argv[4], "notifyAttr")){//set paramter notify attr
					if(!strcasecmp(argv[5], "off")){
						notifyType = 0;
					}else if(!strcasecmp(argv[5], "active")){
						notifyType = 2;
					}else if(!strcasecmp(argv[5], "passive")){
						notifyType = 1;
					}else{
						CMS_LOG(LL_ERROR, "notifyAttr is wrong!!\n");
						goto error;
					}
					if(cmc_setNotifyAttr(argv[2], argv[3], notifyType) != CMS_OK){
						printf("set object %s paramter %s notify attr(%s) fail.\n", argv[2], argv[3], argv[5]);
						goto exit;
					}else{
						printf("set object %s paramter %s notify attr(%s) success.\n", argv[2], argv[3], argv[5]);
					}
				}else{//set paramter value
					cmc_setParameter(argv[2], argv[3], argv[4], argv[5]);
				}
			}else{
				goto error;
			}
			
			break;
		case 'g':
			if((argc != 3) && (argc != 4) && (argc != 5)){
				goto error;
			}
			recordFlag = 1;

			if(argc == 3){
				cmc_getParameter(argv[2], NULL);
			}else if(argc == 4){
				if(!strcasecmp(argv[3], "notifyAttr")){
					cwmp_getParaAttrList_t *paraAttrList = NULL, *curParaAttr = NULL;
					char pathName[256] = {0};
					
					sprintf(pathName, "%s.", argv[2]);
					cmsXmlGetParametereNotifyAttr(xmlShmCb, pathName, &paraAttrList);
					curParaAttr = paraAttrList;
					while(curParaAttr){
						printf("%-8s parameter = %s\n", "", curParaAttr->parameterName);

						if(curParaAttr->notfiyType == 0){
							printf("%-8s notifyAttr= %d(%s)\n", "", curParaAttr->notfiyType, "off");
						}else if(curParaAttr->notfiyType == 2){
							printf("%-8s notifyAttr= %d(%s)\n", "", curParaAttr->notfiyType, "active");
						}else if(curParaAttr->notfiyType == 1)
							printf("%-8s notifyAttr= %d(%s)\n", "", curParaAttr->notfiyType, "passive");

						printf("\r\n");
							
						curParaAttr = curParaAttr->next;
					}
					cmsXmlFreeNotifyAttrList(paraAttrList);
				}else{
					cmc_getParameter(argv[2], argv[3]);
				}
			}else if(argc == 5){
				cwmp_getParaAttrList_t *paraAttrList = NULL, *curParaAttr = NULL;;
				char pathName[256] = {0};
				
				sprintf(pathName, "%s.%s", argv[2], argv[3]);

				cmsXmlGetParametereNotifyAttr(xmlShmCb, pathName, &paraAttrList);

				curParaAttr = paraAttrList;
				while(curParaAttr){
					printf("%-8s parameter = %s\n", "", curParaAttr->parameterName);

					if(curParaAttr->notfiyType == 0){
						printf("%-8s notifyAttr= %d(%s)\n", "", curParaAttr->notfiyType, "off");
					}else if(curParaAttr->notfiyType == 2){
						printf("%-8s notifyAttr= %d(%s)\n", "", curParaAttr->notfiyType, "active");
					}else if(curParaAttr->notfiyType == 1)
						printf("%-8s notifyAttr= %d(%s)\n", "", curParaAttr->notfiyType, "passive");

					printf("\r\n");
						
					curParaAttr = curParaAttr->next;
				}

				cmsXmlFreeNotifyAttrList(paraAttrList);
			}	
			
			break;
		case 'e':
			if(argc != 5){
				goto error;
			}
			recordFlag = 1;
			
			cmc_sendEvent(argv[2], argv[3], argv[4]);
			break;
		case 'u':
			if(argc != 3){
				goto error;
			}
			recordFlag = 1;
			
			cmc_updateObj(argv[2]);
			
			break;
		case 't':
			if((argc != 3) || strcmp(argv[2], "save") ){
				goto error;
			}
			recordFlag = 1;

			cmsXmlSave(xmlShmCb);
			
			break;
		case 'f':
			if(argc != 3){
				goto error;
			}
			recordFlag = 1;
			if(cmc_handleByFile(argv[2]) == -1)
				printf("handle object by file %s fail.\n", argv[2]);

			break;
		case 'l':
			if(argc != 4){
				goto error;
			}
			recordFlag = 1;
			cmsloglevel = atoi(argv[2]);
			strcpy(processName, argv[3]);
			printf("Set debug level, cmsLoglevel %d, process %s\n",cmsloglevel, processName);
			if((cmsloglevel > LL_FLOOD) || (cmsloglevel < LL_FATAL)){
				printf("log level out of range.\n");
				return -1;
			}
			
			if(setProcessLogLevel(processName, cmsloglevel) == -1)
				printf("set process log level fail.\n");

			break;
		case 'c':
			if(argc != 4 && argc != 5){
				goto error;
			}
			recordFlag = 1;
			
			if(!strcmp(argv[2], "e")){//encrypt
				if(cmsXml_openSSL_Enc(argv[3], "/tmp/cmcEnc.bin") == -1)
					CMS_LOG(LL_ERROR, "encry file %s fail.\n", argv[3]);
			}else if(!strcmp(argv[2], "d")){//decrypt
				if(cmsXml_openSSL_Dec(argv[3], "/tmp/cmcDenc.xml") == -1)
					CMS_LOG(LL_ERROR, "dencry file %s fail.\n", argv[3]);
			}else if(!strcmp(argv[2], "l")){/*获取子节点*/
				cmc_getChildList(argv[3], TRUE);
			}else if(!strcmp(argv[2], "c")){
				//CMS_LOG(LL_ERROR, "argc %d, argv[0] %s, argv[1] %s, argv[2] %s, argv[3] %s, argv[4] %s.", argc, argv[0], argv[1], argv[2], argv[3], argv[4]);
				uint32_t objId = 0;
				uint64_t objIA = 0;
				uint32_t restoreType = atoi(argv[4]);
				uint32_t objOff = 0;
				
				void *object = cmsXmlGetObjectByName(xmlShmCb, argv[3]); 
				if(object == NULL){
					CMS_LOG(LL_ERROR, "object is not exist or object is index node without parameters.");
					goto error;
				}
				
				objOff = cmsXmlGetPtrOffset(xmlShmCb, object);
				objId = cmsXml_GetOIDByOffset(xmlShmCb, objOff);
				objIA = cmsXmlGetPreObjIA(xmlShmCb,objOff);
				CMS_LOG(LL_ERROR, "restore object %s(%u, 0x%lx), restoreType %u.", argv[3], objId, objIA, restoreType);
				cmsXml_objValueRestore(xmlShmCb, objId, objIA, restoreType, FALSE, TRUE);
			}else{
				goto error;
			}

			break;
		case 'p':
			if(argc < 3 || argc > 4)
				goto error;
			
			if(strcmp(argv[2], "showShmSize") && strcmp(argv[2], "event") && strcmp(argv[2], "object"))
				goto error;

			recordFlag = 1;
			
			if(argc == 3){
				if(strcmp(argv[2], "event") == 0)
					cmc_printEventInfo(NULL);
				else if(strcmp(argv[2], "showShmSize") == 0){
					uint32_t usedShmSize = cmsXmlGetUsedShmSize(xmlShmCb);
					CMS_LOG(LL_ERROR, "xml current used shm size %u(max support %u)\n", usedShmSize, X2S_MEMORY_SIZE);
				}
				else
					goto error;
			}else if(argc == 4){
				if(strcmp(argv[2], "event") == 0)
					cmc_printEventInfo(argv[3]);
				else if(strcmp(argv[2], "objectid") == 0)
				{
					if(strcmp(argv[3], "all") == 0){
						cmc_getAllObjectIdPath();
					}
					else {
						cmc_getObjectIdPath(atoi(argv[3]));
					}
				}
				else
					cmc_getChildList(argv[3], FALSE);
			}else
				goto error;

			break;
		case 'h':
			recordFlag = 1;
			usage_print();
			break;
		}
	}
	
	if(recordFlag == 0)
		usage_print();

exit:	
	cmc_Deinit();
	return 0;

error:
	usage_print();
	cmc_Deinit();
	return -1;
}
